<template>
  <!-- 机票付款页 -->
  <div class="container">
    <div class="main">
      <div class="pay-title">
        支付总金额
        <span v-if="payData" class="pay-price">￥ {{payData.price}}</span>
      </div>

      <div class="pay-main">
        <h4>微信支付</h4>
        <el-row class="pay-qrcode" type="flex" justify="space-between" align="middle">
          <div class="qrcode">
            <!-- 画布容器-二维码 -->
            <canvas ref="canvas" class="canvas"></canvas>
            <p>请使用微信扫一扫</p>
            <p>扫描二维码支付</p>
          </div>
          <div class="pay-example">
            <img src="http://157.122.54.189:9093/images/wx-sweep2.jpg" alt />
          </div>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
//引入qrcode包
import qrcode from "qrcode";
export default {
  created() {
    //获取付机票订单数据
  },
  data() {
    return {
      payData: null,
      times: null
    };
  },
  //监听器
  watch: {
    //监听token
    "$store.state.user.userInfo.token": {
      handler: function() {
        //获取付机票订单数据
        if (this.$store.state.user.userInfo.token) {
          this.$axios({
            url: "airorders/" + this.$route.query.id,
            headers: {
              Authorization: "Bearer " + this.$store.state.user.userInfo.token
            }
          }).then(res => {
            //赋值
            this.payData = res.data;
            console.log(res.data);

            //生成二维码
            qrcode.toCanvas(this.$refs.canvas, res.data.payInfo.code_url);

            //检测订单是否已经支付完成
            this.loadCheckpau();
          });
        }
      },
      immediate: true
    }
  },
  methods: {
    loadCheckpau() {
      //轮询
      //判断用户是否已经支付了订单金额
      this.$axios({
        url: "/airorders/checkpay",
        method: "post",
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token
        },
        data: {
          id: this.payData.id,
          nonce_str: this.payData.parice,
          out_trade_no: this.payData.payInfo.order_no
        }
      }).then(res => {
        if (res.data.trade_state == "NOTPAY") {
          // console.log(res.data.statusTxt);
          //计时器
          this.times = setTimeout(() => {
            this.loadCheckpau();
          }, 5000);
        } else if (res.data.trade_state == "SUCCESS") {
          // this.$message.success(res.data.statusTxt);
          this.processPayState(res.data.trade_state);
          console.log("支付成功");
          this.destroyed();
        }
        console.log(res);
      });
    },
    //switch方法
    processPayState(state) {
      switch (state) {
        case "SUCCESS":
          this.$message.success("支付已完成");
          break;
        case "REFUND":
          this.$message.success("转入退款");
          break;
        case "NOTPAY":
          this.$message.success("未支付");
          break;
        case "CLOSED":
          this.$message.success("已关闭");
          break;
        case "REVOKED":
          this.$message.success("已撤销");
          break;
        case "USERPAYING":
          this.$message.success("用户支付中");
        case "PAYERROR":
          this.$message.success("支付失败");
          break;
          break;
        default:
          break;
      }
    }
  },
  //vue声明周期钩子--销毁
  destroyed() {
    //页面跳转，清空定时器
    // 跳出页面时应该销毁定时器, 避免堆积
    clearTimeout(this.times);
  }
};
</script>

<style lang="less" scoped>
.container {
  padding: 30px 0;
  background: #f5f5f5;
  .main {
    width: 1000px;
    margin: 0 auto;
    .pay-title {
      text-align: right;
      span {
        font-size: 28px;
        color: orangered;
      }
    }
    .pay-main {
      background: #fff;
      margin-bottom: 10px;
      padding: 30px;
      border-top: 5px solid orange;
      h4 {
        margin-bottom: 10px;
        font-size: 28px;
        font-weight: normal;
      }
      .pay-qrcode {
        padding: 0 80px;
        .qrcode {
          border: 1px solid #ddd;
          padding: 15px;
          height: fit-content;
          .canvas {
            margin-bottom: 10px;
            width: 20px;
            height: 200px;
          }
          p {
            text-align: center;
            line-height: 2;
          }
        }
      }
    }
  }
}
</style>